﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

using Net.SyntaxC4.WindowsAzure.EmailQueue.BLL;

namespace Net.SyntaxC4.WindowsAzure.EmailQueue.Web
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            //TODO: Step 2: Application Begin Request
            HttpApplication app = sender as HttpApplication;
            HttpContext context = app.Context;

            // Used to limit the amount of requests to create
            // Storage Services for our Application
            FirstRequestInitialization.Initialize(context);

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            //TODO: Step ?: Error Debugging
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
        //TODO: Step 1: Internal Class FirstRequest
internal class FirstRequestInitialization
    {
        private static bool _InitializedAlready = false;
        private static object _lock = new Object();

        public static void Initialize(HttpContext context)
        {
            lock (_lock)
            {
                if (_InitializedAlready) return;

                ApplicationStartUponFirstRequest(context);
                _InitializedAlready = true;
            }
        }

        private static void ApplicationStartUponFirstRequest(HttpContext context)
        {
            // Create Table Storage
            ConfigureTableStorage();

            // Create Queue Storage
            ConfigureQueueStorage();

            // Create Blob Storage
            ConfigureBlobStorage();
        }

        private static void ConfigureTableStorage()
        {            
            CloudTableClient cloudTableClient =
                new CloudTableClient(CloudStorageAccount.DevelopmentStorageAccount
                                     .TableEndpoint.ToString(),
                                     CloudStorageAccount.DevelopmentStorageAccount
                                     .Credentials);

            cloudTableClient.CreateTableIfNotExist("Emails");
        }

        private static void ConfigureQueueStorage()
        {
            CloudQueueClient cloudQueueClient =
                CloudStorageAccount.DevelopmentStorageAccount.CreateCloudQueueClient();

            CloudQueue cloudQueue = 
                cloudQueueClient.GetQueueReference(Globals.QueueStorageName);

            cloudQueue.CreateIfNotExist();
        }

        private static void ConfigureBlobStorage()
        {

            CloudBlobClient cloudBlobClient =
                new CloudBlobClient(CloudStorageAccount.DevelopmentStorageAccount
                                    .BlobEndpoint.ToString(),
                                    CloudStorageAccount.DevelopmentStorageAccount
                                    .Credentials);

            cloudBlobClient.GetBlobReference(Globals.BlobStorageName)
                .Container.CreateIfNotExist();
        }
    }

}